package com.chaos.eurekaproducer.controller;

import com.chaos.eurekaproducer.utils.MyThreadPoolExecutor;
import com.chaos.eurekaproducer.utils.ThreadPoolUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.*;

/**
 * @author liaopeng
 * @title: ThreadTestController
 * @projectName eureka-producer
 * @description: TODO
 * @date 2021/5/8上午10:20
 */
@RestController
@RequestMapping("/thread")
public class ThreadTestController {

    @Autowired
    private Executor executor;
    int n=0;
    @RequestMapping("/test")
    @ResponseBody
    private int test() throws Exception {
//        ThreadPoolUtils.fixedThreadPool(new Runnable() {
//            @SneakyThrows
//            @Override
//            public void run() {
//                int m=0;
//                for (int i = 0; i <2000; i++) {
//                     m++;
//                }
//                Thread.sleep(1000);
//                System.out.println("ThreadPool-当前线程名："+Thread.currentThread().getName());
//            }
//        });
        try {
            n=0;
            for (int i = 0; i < 5; i++) {
                executor.execute(new Runnable() {
                    @Override
                    public void run() {
                        if (n==3){
                            try {
                                int k = 1/0;
                            }catch (Exception e){
                                System.out.println("线程异常");
                            }

                        }
                        n++;
                        System.out.println("MyAsyncConfigurer-当前线程名："+Thread.currentThread().getName()+"，n:"+n);
                    }
                });
            }
            System.out.println("结束");
        }catch (Exception e){
            System.out.println("异常");
        }
        return 1;
    }


    public static void main(String[] args){
        ThreadPoolUtils.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("执行线程");
                int i=1/0;
            }
        });

    }
}
